const models = require('../models');
const User = models.User;
const utility = require('utility');
const uuid = require('node-uuid');
const mongoose = require('mongoose');

/**
 * 根据登录名查找用户
 * Callback:
 * - err, 数据库异常
 * - user, 用户
 * @param {String} loginName 登录名
 * @param {Function} callback 回调函数
 */
exports.getUserByLoginName = function (loginName, callback) {
    User.findOne({'loginName': loginName}, callback);
};

/**
 * 根据邮箱，查找用户
 * Callback:
 * - err, 数据库异常
 * - user, 用户
 * @param {String} email 邮箱地址
 * @param {Function} callback 回调函数
 */
exports.getUserByMail = function (email, callback) {
   User.findOne({email: email}, callback);
};

/**
 * 根据用户ID，查找用户
 * Callback:
 * - err, 数据库异常
 * - user, 用户
 * @param {String} id 用户ID
 * @param {Function} callback 回调函数
 */
exports.getUserById = function (id, callback) {
    if(!id) {
        return callback();
    }

    User.findOne({_id: id}, callback);
};

/**
 * 根据user_name or user_id，获取用户
 * Callback:
 * - err, 数据库异常
 * - users, 用户列表
 * @param {String} query 关键字
 * @param {Object} opt 选项
 * @param {Function} callback 回调函数
 */
exports.getUser = function(query, opt, callback) {
    User.findOne(query, '', opt, callback);
};

//根据用户名查询
exports.getUserByName = function(loginName, callback) {
    User.findOne({loginName: loginName}, callback);
};


/**
 * 根据关键字，获取一组用户
 * Callback:
 * - err, 数据库异常
 * - users, 用户列表
 * @param {String} query 关键字
 * @param {Object} opt 选项
 * @param {Function} callback 回调函数
 */
exports.getUsersByQuery = function(query, opt, callback) {
    User.find(query, '', opt, callback);
};

exports.newAndSave  = function (name, loginName, pass, email, avatar_url='', active, callback) {
    let user         = new User();
    user.name        = loginName;
    user.loginName   = loginName;
    user.pass        = pass;
    user.email       = email;
    user.avatar      = avatar_url;
    user.active      = active || false;
    user.accessToken = uuid.v4();

    user.save(callback);
};